package com.songlanyun.modules.article.entity;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import com.songlanyun.common.drive.RootEntity;
import com.songlanyun.common.enums.ArticleConstant;
import com.songlanyun.common.enums.CommentConstant;
import com.songlanyun.modules.article.model.dto.ArticleDTO;
import com.songlanyun.modules.article.service.ArticleContentService;
import com.songlanyun.modules.article.service.ArticleService;
import com.songlanyun.modules.comment.entity.UserComment;
import com.songlanyun.modules.comment.service.UserCommentService;
import com.songlanyun.modules.opus.service.OpusService;
import com.songlanyun.modules.rabbitmq.util.SpringContextUtil;
import lombok.Data;

/**
 * 文章表
 *
 * @author wuchao
 * @email sunlightcs@gmail.com
 * @date 2021-02-27 15:36:01
 */
@Data
@TableName("yxt_article")
public class Article extends RootEntity<Long, Article> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     *
     */
    @TableId
    private Long id;
    /**
     * 文章标题
     */
    private String title;
    /**
     * 课程id
     */
    @TableField(exist = false)
    private Long courseId;
    /**
     * 文章封面
     */
    private String cover;
    /**
     * 文章类型
     * 0：图文
     * 1：视频
     */
    private ArticleConstant.ArticleType articleType;

    /**
     * 是否显示
     */
    private Boolean isShow;
    /**
     * 是否推荐
     */
    private Boolean isRecommend;
    /**
     * 上下架
     */
    private Boolean added;
    /**
     * 创建人ID
     */
    private Long creator;
    /**
     * 创建时间（发布时间）
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    /**
     * 修改时间
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;

    /**
     * 初始阅读人数
     */
    private Integer initBrowseNum;
    /**
     * 初始点赞人数
     */
    private Integer initLikeNum;
    /**
     * 初始分享人数
     */
    private Integer initShareNum;

    /**
     * 初始化总数
     */
    public void initSum() {
        this.browseNum = this.initBrowseNum;
        this.likeNum = this.initLikeNum;
        this.shareNum = this.initShareNum;
        this.browseSum = this.initBrowseNum;
        this.shareSum = this.initShareNum;
    }

    /**
     * 更新初始化总数
     */
    public void updateInitSum(ArticleDTO dto) {
        this.browseNum = dto.getInitBrowseNum() + (this.browseNum - this.initBrowseNum);
        this.likeNum = dto.getInitLikeNum() + (this.likeNum - this.initLikeNum);
        this.shareNum = dto.getInitShareNum() + (this.shareNum - this.initShareNum);
        this.browseSum = dto.getInitBrowseNum() + (this.browseSum - this.initBrowseNum);
        this.shareSum = dto.getInitShareNum() + (this.shareSum - this.initShareNum);

    }


    /**
     * 浏览人数
     */
    private Integer browseNum;

    public void addBrowseNum(Integer num) {
        this.browseNum = this.browseNum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 点赞人数
     */
    private Integer likeNum;

    /**
     * 增加点赞数量
     *
     * @param num
     */
    public void addLikeNum(Integer num) {
        this.likeNum = this.likeNum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 减少点赞数量
     *
     * @param num
     */
    public void subLikeNum(Integer num) {
        if (this.likeNum > num) {
            this.likeNum = this.likeNum - num;
        } else {
            this.likeNum = 0;
        }
        loadArticleService().updateById(this);

    }

    /**
     * 分享人数
     */
    private Integer shareNum;

    public void addShareNum(Integer num) {
        this.shareNum = this.shareNum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 评论人数
     */
    private Integer commentNum;

    public void addCommentNum(Integer num) {
        this.commentNum = this.commentNum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 浏览总数
     */
    private Integer browseSum;

    public void addBrowseSum(Integer num) {
        this.browseSum = this.browseSum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 评论总数
     */
    private Integer commentSum;

    public void addCommentSum(Integer num) {
        this.commentSum = this.commentSum + num;
        loadArticleService().updateById(this);
    }

    /**
     * 分享总数
     */
    private Integer shareSum;

    public void addShareSum(Integer num) {
        this.shareSum = this.shareSum + num;
        loadArticleService().updateById(this);
    }

    public ArticleService loadArticleService() {
        return (ArticleService) SpringContextUtil.getBean("articleService");
    }

    @Override
    public Long getIdentity() {
        return this.getId();
    }
}
